package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.security.domain.Security;
import com.ruoyi.security.dto.SecurityDto;
import com.ruoyi.security.dto.SyncSecurityDayMinuteTradeDto;
import com.ruoyi.security.service.ISecurityDayMinuteTradeService;
import com.ruoyi.security.service.ISecurityService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 证券定时任务调度
 * 
 * @author ruoyi
 */
@Component("securityTask")
public class SecurityTask {

    @Resource
    private ISecurityDayMinuteTradeService securityDayMinuteTradeService;

    @Resource
    private ISecurityService securityService;

    public void syncSecurityDayMinuteTrade(String symbol) {
        SyncSecurityDayMinuteTradeDto dto = new SyncSecurityDayMinuteTradeDto();
        dto.setSymbol(symbol);

        LocalDate startDate = LocalDate.now().minusYears(1).minusDays(2);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedStartDate = startDate.format(formatter);
        dto.setStartDate(formattedStartDate);

        dto.setEndDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.getNowDate()));
        securityDayMinuteTradeService.syncList(dto);
    }

    public void syncAllSecurityDayMinuteTrade() {
        List<Security> securities = securityService.list(new SecurityDto());
        for (Security security : securities) {
            syncSecurityDayMinuteTrade(security.getSymbol());
        }
    }

}
